package  com.rboat.exam.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rboat.common.annotation.Log;
import com.rboat.common.enums.BusinessType;
import com.rboat.exam.domain.ExamTestLibrary;
import com.rboat.exam.service.IExamTestLibraryService;
import com.rboat.common.core.controller.BaseController;
import com.rboat.common.core.domain.AjaxResult;
import com.rboat.common.utils.poi.ExcelUtil;
import com.rboat.common.core.page.TableDataInfo;

/**
 * 题库Controller
 * 
 * @author rboat
 * @date 2019-09-02
 */
@Controller
@RequestMapping("/exam/library")
public class ExamTestLibraryController extends BaseController
{
    private String prefix = "exam/library";

    @Autowired
    private IExamTestLibraryService examTestLibraryService;

    @RequiresPermissions("exam:library:view")
    @GetMapping()
    public String library()
    {
        return prefix + "/library";
    }

    /**
     * 查询题库列表
     */
    @RequiresPermissions("exam:library:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamTestLibrary examTestLibrary)
    {
        startPage();
        List<ExamTestLibrary> list = examTestLibraryService.selectExamTestLibraryList(examTestLibrary);
        return getDataTable(list);
    }

    /**
     * 导出题库列表
     */
    @RequiresPermissions("exam:library:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamTestLibrary examTestLibrary)
    {
        List<ExamTestLibrary> list = examTestLibraryService.selectExamTestLibraryList(examTestLibrary);
        ExcelUtil<ExamTestLibrary> util = new ExcelUtil<ExamTestLibrary>(ExamTestLibrary.class);
        return util.exportExcel(list, "library");
    }

    /**
     * 新增题库
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存题库
     */
    @RequiresPermissions("exam:library:add")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamTestLibrary examTestLibrary)
    {
        return toAjax(examTestLibraryService.insertExamTestLibrary(examTestLibrary));
    }

    /**
     * 修改题库
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ExamTestLibrary examTestLibrary = examTestLibraryService.selectExamTestLibraryById(id);
        mmap.put("examTestLibrary", examTestLibrary);
        return prefix + "/edit";
    }

    /**
     * 修改保存题库
     */
    @RequiresPermissions("exam:library:edit")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamTestLibrary examTestLibrary)
    {
        return toAjax(examTestLibraryService.updateExamTestLibrary(examTestLibrary));
    }

    /**
     * 删除题库
     */
    @RequiresPermissions("exam:library:remove")
    @Log(title = "题库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examTestLibraryService.deleteExamTestLibraryByIds(ids));
    }
}
